package org.millenaire_reborn;

import java.nio.file.Path;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import com.electronwill.nightconfig.core.file.CommentedFileConfig;
import com.electronwill.nightconfig.core.io.WritingMode;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.neoforge.common.ModConfigSpec;

// An example config class. This is not required, but it's a good idea to have one to keep your config organized.
// Demonstrates how to use Neo's config APIs
public class MillConfig
{
    private static final ModConfigSpec.Builder BUILDER = new ModConfigSpec.Builder();
    static final ModConfigSpec SPEC = BUILDER.build();

    public static boolean learnLanguages;
    public static boolean villageAnnouncement;
    public static boolean displayNames;
    public static int nameDistance;
    public static int dialogueDistance;

    public static boolean generateVillages;
    public static boolean generateLoneBuildings;
    public static int minVillageDistance;
    public static int minLoneDistance;
    public static int minVillageLoneDistance;
    public static int spawnDistance;

    public static int loadedRadius;
    public static int minBuildingDistance;
    public static int maxChildren;
    public static boolean buildPaths;
    public static int villageRelationDistance;
    public static int banditRaidDistance;
    public static int raidPercentChance;
    //public static Set<Block> forbiddenBlocks;

    public static final String CATEGORYUIOPTIONS = "ctgy_uioptions";
    public static final String CATEGORYWORLDGEN = "ctgy_worldgen";
    public static final String CATEGORYVILLAGEBEV = "ctgy_villagebehavior";

    public static final ModConfigSpec.BooleanValue LEARN_LANGUAGES = BUILDER
            .comment(CATEGORYUIOPTIONS)
            .define("learnLanguages", true);
    public static final ModConfigSpec.BooleanValue VILLAGE_ANNOUNCEMENT = BUILDER
            .comment(CATEGORYUIOPTIONS)
            .define("villageAnnouncement", false);
    public static final ModConfigSpec.BooleanValue DISPLAY_NAMES = BUILDER
            .comment(CATEGORYUIOPTIONS)
            .define("displayNames", true);
    public static final ModConfigSpec.IntValue NAME_DISTANCE = BUILDER
            .comment(CATEGORYUIOPTIONS)
            .defineInRange("nameDistance", 20, 0, Integer.MAX_VALUE);
    public static final ModConfigSpec.IntValue DIALOGUE_DISTANCE = BUILDER
            .comment(CATEGORYUIOPTIONS)
            .defineInRange("dialogueDistance", 5, 0, Integer.MAX_VALUE);

    public static final ModConfigSpec.BooleanValue GENERATE_VILLAGES = BUILDER
            .comment(CATEGORYWORLDGEN)
            .define("generateVillages", true);
    public static final ModConfigSpec.BooleanValue GENERATE_LONE_BUILDINGS = BUILDER
            .comment(CATEGORYWORLDGEN)
            .define("generateLoneBuildings", true);
    public static final ModConfigSpec.IntValue MIN_VILLAGE_DISTANCE = BUILDER
            .comment(CATEGORYWORLDGEN)
            .defineInRange("minVillageDistance", 600, 0, Integer.MAX_VALUE);
    public static final ModConfigSpec.IntValue MIN_LONE_DISTANCE = BUILDER
            .comment(CATEGORYWORLDGEN)
            .defineInRange("minLoneDistance", 600, 0, Integer.MAX_VALUE);
    public static final ModConfigSpec.IntValue MIN_VILLAGE_LONE_DISTANCE = BUILDER
            .comment(CATEGORYWORLDGEN)
            .defineInRange("minVillageLoneDistance", 300, 0, Integer.MAX_VALUE);
    public static final ModConfigSpec.IntValue SPAWN_DISTANCE = BUILDER
            .comment(CATEGORYWORLDGEN)
            .defineInRange("spawnDistance", 200, 0, Integer.MAX_VALUE);

    public static final ModConfigSpec.IntValue LOADED_RADIUS = BUILDER
            .comment(CATEGORYVILLAGEBEV)
            .defineInRange("loadedRadius", 200, 0, Integer.MAX_VALUE);
    public static final ModConfigSpec.IntValue MIN_BUILDING_DISTANCE = BUILDER
            .comment(CATEGORYVILLAGEBEV)
            .defineInRange("minBuildingDistance", 2, 0, Integer.MAX_VALUE);
    public static final ModConfigSpec.IntValue MAX_CHILDREN = BUILDER
            .comment(CATEGORYVILLAGEBEV)
            .defineInRange("maxChildren", 10, 0, Integer.MAX_VALUE);
    public static final ModConfigSpec.BooleanValue BUILD_PATHS = BUILDER
            .comment(CATEGORYVILLAGEBEV)
            .define("buildPaths", true);
    public static final ModConfigSpec.IntValue VILLAGE_RELATION_DISTANCE = BUILDER
            .comment(CATEGORYVILLAGEBEV)
            .defineInRange("villageRelationDistance", 2000, 0, Integer.MAX_VALUE);
    public static final ModConfigSpec.IntValue BANDIT_RAID_DISTANCE = BUILDER
            .comment(CATEGORYVILLAGEBEV)
            .defineInRange("banditRaidDistance", 1500, 0, Integer.MAX_VALUE);
    public static final ModConfigSpec.IntValue RAID_PERCENT_CHANCE = BUILDER
            .comment(CATEGORYVILLAGEBEV)
            .defineInRange("raidPercentChance", 20, 0, Integer.MAX_VALUE);
    /*
    private static final ModConfigSpec.ConfigValue<List<? extends String>> FORBIDDEN_BLOCK = BUILDER
            .comment(CATEGORYVILLAGEBEV)
            .defineListAllowEmpty("forbiddenBlocks", List.of("minecraft:bedrock"), MillConfig::validateBlockName);*/

    private static boolean validateBlockName(final Object obj)
    {
        return obj instanceof String itemName && BuiltInRegistries.BLOCK.containsKey(ResourceLocation.parse(itemName));
    }

    /*
    static void onLoad(final ModConfigEvent event)
    {
        SPEC = BUILDER.build();
        learnLanguages = LEARN_LANGUAGES.get();
        villageAnnouncement = VILLAGE_ANNOUNCEMENT.get();
        displayNames = DISPLAY_NAMES.get();
        nameDistance = NAME_DISTANCE.get();
        dialogueDistance = DIALOGUE_DISTANCE.get();
        generateVillages = GENERATE_VILLAGES.get();
        generateLoneBuildings = GENERATE_LONE_BUILDINGS.get();
        minVillageDistance = MIN_VILLAGE_DISTANCE.get();
        minLoneDistance = MIN_LONE_DISTANCE.get();
        minVillageLoneDistance = MIN_VILLAGE_LONE_DISTANCE.get();
        spawnDistance = SPAWN_DISTANCE.get();
        loadedRadius = LOADED_RADIUS.get();
        minBuildingDistance = MIN_BUILDING_DISTANCE.get();
        maxChildren = MAX_CHILDREN.get();
        buildPaths = BUILD_PATHS.get();
        villageRelationDistance = VILLAGE_RELATION_DISTANCE.get();
        banditRaidDistance = BANDIT_RAID_DISTANCE.get();
        raidPercentChance = RAID_PERCENT_CHANCE.get();
        /*
        forbiddenBlocks = FORBIDDEN_BLOCK.get().stream()
                .map(blockName -> BuiltInRegistries.BLOCK.getValue(ResourceLocation.parse(blockName)))
                .collect(Collectors.toSet());*/
   /* }*/
}
